package org.tp8.sampleapp.security.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.tp8.sampleapp.security.persistence.repository.RoleRepository;
import org.tp8.sampleapp.security.persistence.repository.UserRepository;

/**
 * The Class UserDetailsServiceImpl.
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

	/** The user repository. */
	@Autowired
	private UserRepository userRepository;

	/** The role repository. */
	@Autowired
	private RoleRepository roleRepository;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#
	 * loadUserByUsername(java.lang.String)
	 */
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		UserDetails user = userRepository.findByUsername(username);
		if (user == null) {
			throw new UsernameNotFoundException(username);
		}
		return user;
	}

}
